package com.d3works.apps.base.service.currency;

import com.d3works.apps.base.exceptions.BaseExceptionMessage;
import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.google.common.base.Strings;
import com.google.inject.Inject;

public class CurrencyConversionFactory {

  protected AppBaseService appBaseService;

  @Inject
  public CurrencyConversionFactory(AppBaseService appBaseService) {
    this.appBaseService = appBaseService;
  }

  public CurrencyConversionService getCurrencyConversionService() throws D3worksException {

    String currencyWsurl = appBaseService.getAppBase().getCurrencyWsURL();

    if (Strings.isNullOrEmpty(currencyWsurl)) {
      throw new D3worksException(
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(BaseExceptionMessage.CURRENCY_8));
    }
    try {
      return (CurrencyConversionService) Beans.get(Class.forName(currencyWsurl));
    } catch (ClassNotFoundException | ClassCastException e) {
      throw new D3worksException(
          TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(BaseExceptionMessage.CURRENCY_9));
    }
  }
}
